package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"github.com/gin-gonic/gin"
	"github.com/lzppppppp/virapaksa-go/internal/monitor_check_agent/service"
	"github.com/spf13/viper"
)

func main() {

	logFilePath := flag.String("log_path", "./log/", "Path to the log file")
	listenPort := flag.String("listen_port", "7777", "Listen Port")
	flag.Parse()

	InitConfig(*logFilePath)

	r := CollectRoute(gin.Default())

	panic(r.Run(":" + *listenPort))

}

func CollectRoute(r *gin.Engine) *gin.Engine {

	v1 := r.Group("/v1")
	{
		v1.POST("/check/portAgent", service.PostPortCheckAgent)
		v1.POST("/check/keywordAgent", service.PostKeywordCheckAgent)
	}
	return r
}

func InitConfig(path string) {

	// log库初始化
	file := viper.GetString("log_path") + "/message.log"
	logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
	if err != nil {
		fmt.Println("errrrrrrrr")
		return
	}
	log.SetOutput(logFile)
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
